﻿Option Strict On
Option Explicit On
Option Infer On

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Xml.Linq
Imports GenDotNet.Task.Common
Imports GenDotNet.model.common
Imports <xmlns:orm="http://kadgen.com/KADORM.xsd">
Imports <xmlns:kd="http://kadgen.com/Special.xsd">
Imports <xmlns:s="http://kadgen.com/Select.xsd">

Namespace GenInputMetadata

   Public Class RootData
      Implements IRootData

      Private mObjects As Dictionary(Of String, ObjectData)

      Public Shared Function LoadData( _
               ByVal xDoc As XDocument) _
               As IRootData
         ' This is the load entrypoint
         Dim rootData = New RootData()
         rootData.mObjects = New Dictionary(Of String, ObjectData)
         Dim root = xDoc.Root
         For Each objectElement In root...<orm:Object>
            rootData.mObjects.Add(objectElement.@Name, ObjectData.Load(rootData, objectElement))
         Next
         Return rootData
      End Function

      Public Function GetXList() _
               As XElement _
               Implements IRootData.GetXList
         Return _
<s:Objects>
   <%= From obj In mObjects.Values _
      Select <s:Object <%= obj.Element.Attributes %>/> %>
</s:Objects>
      End Function

      Public Function GetItem( _
               ByVal name As String) _
               As IBaseData _
               Implements IRootData.GetItem
         Return GetObject(name)
      End Function

      Public Function GetObject( _
               ByVal name As String) _
               As ObjectData
         Dim data As ObjectData = Nothing
         mObjects.TryGetValue(name, data)
         Return data
      End Function

   End Class

End Namespace
